package ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.presentation.controller.user;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import org.springframework.beans.factory.annotation.Autowired;

import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.presentation.controller.user.devicedata.AlarmBean;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.presentation.controller.util.PresentationUtil;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.DeviceService;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.UserDataService;

/**
 * 
 * Este Bean es utilizado en las vistas
 * simpleuser/alarms.xhtml
 */
@ManagedBean(name="activeAlarmsBean")
@ViewScoped
public class ActiveAlarmsBean {

	@ManagedProperty(value="#{usuarioBean.userDataService}")
	@Autowired(required = true)
	private UserDataService userDataService;
	
	@PostConstruct
	public void init(){
		for (DeviceService serv : userDataService.findDevices()) {
			PresentationUtil.addDeviceToCurrentView(serv);
		}
		
	}
	
	@PreDestroy
	public void finalize(){
		for (DeviceService serv : userDataService.findDevices()) {
			PresentationUtil.removeDeviceToCurrentView(serv);
		}
	}
	
	/**
	 * @return the userDataService
	 */
	public UserDataService getUserDataService() {
		return userDataService;
	}

	/**
	 * @param userDataService the userDataService to set
	 */
	public void setUserDataService(UserDataService userDataService) {
		this.userDataService = userDataService;
	}

	/**
	 * @return the alarms
	 */
	public List<AlarmBean> getAlarms() {
		return AlarmBean.wrap(userDataService.getActiveAlarmsService());
	}

}
